package com.ericom.webapp.action;

import java.util.List;

import com.ericom.model.Member;
import com.ericom.service.GenericManager;

public class MemberAction extends BaseAction {
	private GenericManager<Member, Long> memberManager;
	private List members;
	private Member member;
	private Long id;

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setMemberManager(GenericManager<Member, Long> memberManager) {
		this.memberManager = memberManager;
	}

	public String list() {
		members = memberManager.getAll();
		return SUCCESS;
	}

	public List getMembers() {
		return members;
	}

	public String delete() {
		memberManager.remove(member.getId());
		this.saveMessage(getText("member.deleted"));
		return this.SUCCESS;
	}

	public String edit() {
		if (id != null) {
			member = memberManager.get(id);
		} else {
			member = new Member();
		}
		return this.SUCCESS;
	}

	public String save() throws Exception {
		if (this.cancel != null) {
			return this.CANCEL;
		}
		if (this.delete != null) {
			return this.delete();
		}

		boolean isNew = (member.getId() == null);
		member = memberManager.save(member);

		String key = (isNew) ? "member.added" : "member.updated";
		this.saveMessage(getText(key));

		if (!isNew) {
			return this.INPUT;
		} else {
			return this.SUCCESS;
		}
	}
}
